// pages/application/bmtx.js
import util from "../../utils/util.js";
const app = getApp();
const http = require('../../utils/http.js');
import {
	refreshPage
} from '../../utils/common'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
    HTTP: http.ipRoot,
		id: null,
		name: '',
		idArray: ['id1', 'id2', 'id3'],
		type: null,
		idType: '',
		phone: '',
		address: '',
		programArray: ['节目type1', '节目type2', '节目type3'],
		programType: '',
		programTypeValue: null,
		sqname: '',
		programName: '',
		fileUrl: '',

		maxSize: 200,
		videoName: '',
		videoSize: 0,
		videoUrl: '',
		videoImg: '',
		videoImgUrl: '',
		videoDuration: 0,
		progress: 0,
		showProgress: false,
		dataType: 0,
		isShowProgress: false,
		isSubmit: 1
	},
	getType() {
		http.post('api/other/enum').then(res => {
			var idArray = Object.entries(res.data.wk_type_enum).map(([key, value]) => {
				var obj = {
					'key': key,
					'value': value
				};
				return obj;
			});
			var programArray = Object.entries(res.data.wk_cate_enum).map(([key, value]) => {
				var obj = {
					'key': key,
					'value': value
				};
				return obj;
			});
			this.setData({
				idArray,
				programArray
			})

		})
	},
	changeInput(e) {
		var name = e.currentTarget.dataset.name;
		this.setData({
			[name]: e.detail.value
		})
	},
	bindPickerIdChange(e) {
		this.setData({
			idType: this.data.idArray[e.detail.value].value,
			type: this.data.idArray[e.detail.value].key
		})
	},
	bindRegionChange: function(e) {
		this.setData({
			address: e.detail.value
		})
	},
	bindPickerProgramChange(e) {
		this.setData({
			programType: this.data.programArray[e.detail.value].value,
			programTypeValue: this.data.programArray[e.detail.value].key,
		})
	},
	closeProgress() {
		this.setData({
			showProgress: false,
			videoName: '',
			videoSize: 0,
			videoUrl: '',
			videoImgUrl: '',
			fileUrl: '',
			progress: 0,
			videoDuration: 0,
			isShowProgress: 0
		})
	},
	afterRead() {
		var that = this;
		// 设置视频源
		wx.chooseMedia({
			count: 1,
			mediaType: ['video'],
			sourceType: ['album', 'camera'],
			maxDuration: 60,
			camera: 'back',
			size: 200,
			success(res) {
				wx.showLoading();
				var file = res.tempFiles[0];
				var videoName = '视频.' + file.tempFilePath.split('.')[1];
				that.setData({
					showProgress: true,
					videoName: file.name ? file.name : videoName,
					videoSize: (file.size / 1024 / 1024).toFixed(1) + 'MB',
					videoUrl: file.tempFilePath,
					videoDuration: file.duration,
					videoImg: file.thumbTempFilePath,
					isShowProgress: 1
				})
				if(file.duration > 300){
					wx.hideLoading();
					return wx.showModal({
						title: '',
						content: '单个视频不能超过5分钟，请重新上传~',
						success: (res) => {
							that.closeProgress();
						}
					})
				}
				
				http.uploadFile('api/common/upload', {
					path: res.tempFiles[0].tempFilePath
				}).then(data => {
					wx.hideLoading();
					if (data.code == 1) {
						that.setData({
							progress: 50,
							fileUrl: data.data.url
						})
						
					}else{
						return wx.showModal({
								title: '视频上传失败~',
								content: data.msg,
								success: (res) => {
									that.closeProgress();
								}
							})
							
					}

				}).then(r => {
					http.uploadFile('api/common/upload', {
						path: file.thumbTempFilePath
					}).then(data => {
						if(data.code === 1){
							wx.showToast({
								title: '视频上传成功~'
							})
							that.setData({
								progress: 100,
								videoImgUrl: data.data.url,
								isShowProgress: 0
							})
						}else{
							wx.showModal({
								title: '',
								content: '视频上传失败~'+data.msg
							})
							that.closeProgress();
						}
						
						
	
					})
				})
			},
			fail(e){
				wx.showModal({
					title: '视频上传失败~',
					content: e
				})
				that.closeProgress();
				
			}
		})

	},
	goExplain() {
		wx.navigateTo({
			url: '/pages/explain/index',
		})
	},
	onSubmit() {
		var that = this;
		var data = this.data;
		var videoImgUrl = data.videoImgUrl.indexOf('uploads') != -1 ? data.videoImgUrl.substring(data.videoImgUrl.indexOf('uploads') - 1) : data.videoImgUrl;
		var fileUrl = data.fileUrl.indexOf('uploads') != -1 ? data.fileUrl.substring(data.fileUrl.indexOf('uploads') - 1) : data.fileUrl;
		var postData = {
			id: that.data.id,
			type: data.type,
			province: data.address[0],
			city: data.address[1],
			area: data.address[2],
			contact: data.name,
			tel: data.phone,
			com_name: data.sqname,
			wk_name: data.programName,
			wk_category: data.programTypeValue,
			wk_imgurl: videoImgUrl,
			wk_videourl: fileUrl,
			wk_time: data.videoDuration
		}
		if (postData.type && postData.province && postData.city && postData.area && postData.contact && postData
			.tel && postData.wk_name && postData.wk_category && postData.wk_videourl) {
			if (!app.isPhone(postData.tel)) {
				wx.showToast({
					title: '请填写正确的手机号！',
					icon: 'none'
				})
			}else {
				that.setData({
					isSubmit: 0
				})
				http.post('api/user/ac_works', postData, 1).then(res => {
					if (res.code == 1) {
						wx.showToast({
							title: '提交成功！'
						})
						// refreshPage();
						setTimeout(function(){
							that.setData({
								isSubmit: 1
							})
							that.onBack();
						},4000)
					}

				})
			}
		} else {
			if(!postData.contact){
				return this.getErrMsg('请填写联系人！')
			}
			if(!postData.type){
				return this.getErrMsg('请选择身份类型！')
			}
			if(!postData.tel){
				return this.getErrMsg('请填写联系方式！')
			}
			if(!postData.province){
				return this.getErrMsg('请选择所属省市区！')
			}
			if(!postData.wk_category){
				return this.getErrMsg('请选择节目类型！')
			}
			if(!postData.wk_name){
				return this.getErrMsg('请填写节目名称！')
			}
			if(!postData.wk_videourl){
				return this.getErrMsg('请选择视频！')
			}
		}
	},
	getErrMsg(msg){
		
		return wx.showToast({
			title: msg,
			icon: 'error'
		})
	},
	//报告详情
	getInfoData(id){
		http.post('api/user/my_works_detail',{id}).then(res => {
			var idData = this.data.idArray.filter(re => re.key == res.data.type)[0];
			var progressData = this.data.programArray.filter(re => re.key == res.data.type)[0];
			this.setData({
				name: res.data.contact,
				idType: idData.value,
				type: parseInt(res.data.type),
				['address[0]']: res.data.province,
				['address[1]']: res.data.city,
				['address[2]']: res.data.area,
				phone: res.data.tel,
				sqname: res.data.com_name,
				programName: res.data.wk_name,
				programTypeValue: parseInt(res.data.wk_category),
				programType: progressData.value,
				videoImgUrl: res.data.wk_imgurl,
				videoImg: res.data.wk_imgurl,
				videoUrl: res.data.wk_videourl,
				fileUrl: res.data.wk_videourl,
				videoDuration: res.data.wk_time,
				progress: res.data.wk_videourl ? 100 : 0,
				showProgress: res.data.wk_videourl ? 1 : 0,
				videoName: '视频.mp4'
			})
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.getType();
		if(options.type){
			this.setData({
				dataType: options.type,
				id: options.id
			})
			this.getInfoData(options.id);
		}
		
		
	},
	onBack() {
		wx.navigateBack();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})